//
//  JXLocalizationTools.swift
//  GodoxFlash
//
//  Created by godox on 2021/12/1.
//

import UIKit

public class JXLocalizationTools: NSObject {

    fileprivate static let kSharedSettingsKey = "DefaultUserSettings"
    
    public var language: Language
    
    public enum Language: String {
        
        case chinese = "zh-Hans"
        case english = "en"
        case tradition = "zh-Hant"
        
        var code: String {
            
            return rawValue
        }
    }

    // MARK: 创建单例
    public static let shared: JXLocalizationTools = {
        let appSettings: JXLocalizationTools
        if let savedData = UserDefaults.standard.object(forKey: JXLocalizationTools.kSharedSettingsKey) as? Data,
            let defaultSettings = NSKeyedUnarchiver.unarchiveObject(with: savedData) as? JXLocalizationTools {
            appSettings = defaultSettings
        } else {
            appSettings = JXLocalizationTools()
        }
        return appSettings
    }()
    
    // MARK: 获取键值
    public func valueWithKey(key: String!) -> String {
        let str = NSLocalizedString(key, comment: "")
        return str
    }

    // MARK: - 保存用户当前语言
    static func saveSharedInstance() {
        let data = NSKeyedArchiver.archivedData(withRootObject: JXLocalizationTools.shared)
        UserDefaults.standard.set(data, forKey: JXLocalizationTools.kSharedSettingsKey)
    }

    static func localeIsChinese() -> Language {
        if let lang = Locale.preferredLanguages.first {
            if lang.hasPrefix("zh") {
                if lang.contains("Hant") || lang.contains("Trad") {
                    return .tradition
                }
                return .chinese
            } else if lang.contains("en") {
                return .english
            } else {
                return .english
            }
        } else {
            return .english
        }
    }

    // MARK: 首次进入语言
    override init() {
        language = JXLocalizationTools.localeIsChinese()
        super.init()
    }

}

private var bundleByLanguageCode: [String: Foundation.Bundle] = [:]

extension JXLocalizationTools.Language {
    var bundle: Foundation.Bundle? {
        if let bundle = bundleByLanguageCode[code] {
            return bundle
        } else {
            let mainBundle = Foundation.Bundle.main
            if let path = mainBundle.path(forResource: code, ofType: "lproj"),
                let bundle = Foundation.Bundle(path: path) {
                bundleByLanguageCode[code] = bundle
                return bundle
            } else {
                return nil
            }
        }
    }
}

// 当调用JXLocalizationTools.Language切换语言后替换掉mainBundle为当前语言的bundle
public class JXBundle: Foundation.Bundle {
    public override func localizedString(forKey key: String, value: String?, table tableName: String?) -> String {
        if let bundle = JXLocalizationTools.shared.language.bundle {
            return bundle.localizedString(forKey: key, value: value, table: tableName)
        } else {
            return super.localizedString(forKey: key, value: value, table: tableName)
        }
    }
}

